package com.pan.cuiniubi.create.dao;

import com.pan.cuiniubi.entity.*;
import com.pan.cuiniubi.enumn.LaminaEnum;
import com.pan.cuiniubi.enumn.OperationEnum;
import com.pan.cuiniubi.utils.ReadYamlTool;

import java.util.List;
import java.util.stream.Collectors;

public class CreateDeleteDaoMapping extends CreateDaoMappingImpl {
    @Override
    public List<MethodInfo> getMethodInfo(Table table) {
        List<Member> memberList = table.getMemberList();
        List<MethodInfo> result = memberList.stream().map(member -> {
            return new MethodInfo().setReturnStr(Integer.class.getName())
                    .setParameterList(member)
                    .setMethodName("deleteBy"+member.getMemberName().substring(0,1).toUpperCase()
                            + member.getMemberName().substring(1));
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public DaoInfo getDaoInfo(Table table) {
        DaoInfo daoInfo = new DaoInfo();
        daoInfo.setClassName(getClassName(table.getTableName(), OperationEnum.delete))
                .setMethodInfoList(getMethodInfo(table))
                .setImportStr(getImportString(table))
                .setPackageStr(getPackageStr());
        return daoInfo;
    }

    public List<SqlInfo> getSqlInfo(Table table){
        String tableName =  table.getTableName();
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path");
        List<Member> memberList = table.getMemberList();
        List<SqlInfo> sqlInfoList = memberList.stream().map(member -> {
            SqlInfo sqlInfo = new SqlInfo();
            sqlInfo.setOpt(OperationEnum.delete.name());
//            sqlInfo.setResult(path+"."+tableName);
            sqlInfo.setId("deleteBy"+member.getMemberName().substring(0,1).toUpperCase()
                    +member.getMemberName().substring(1));
            sqlInfo.setSql("delete from "+tableName+" where "+member.getMemberName()+"=#{"+member.getMemberName()+"};");
            return sqlInfo;
        }).collect(Collectors.toList());
        return sqlInfoList;
    }

    public MappingInfo getMappingInfo(Table table){
        MappingInfo mappingInfo = new MappingInfo();
        mappingInfo.setNamespace(getNamespace(table.getTableName(),OperationEnum.delete));
        mappingInfo.setSqlInfoList(getSqlInfo(table));
        String className = getClassName(table.getTableName(),OperationEnum.delete);
        mappingInfo.setClassName(className);
        return mappingInfo;
    }

}
